//
//  ByTabBar.m
//  caipiao_ios
//
//  Created by mc on 2018/7/3.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByTabBar.h"

@implementation ByTabBar
{
    UIControl *_selectedView;       // 当前选中的barview
}

- (id)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


- (UIColor *)tintColor {
    
    if (!_tintColor) {
        _tintColor = [UIColor lightGrayColor];
    }
    return _tintColor;
}
- (UIColor *)selectedTintColor{
    
    if (!_selectedTintColor) {
        _selectedTintColor = BY_THEME_COLOR;
    }
    return _selectedTintColor;
}


- (void)setupUI {
    NSAssert(_images.count == _selectedImages.count, @"常态图片数量必须和高亮图片数量一致");
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.top.equalTo(self).offset(0);
        make.height.mas_equalTo(@1);
    }];
    CGFloat width = SCREEN_WIDTH / self.titles.count;
    for (int i = 0; i < self.titles.count;i++) {
        UIControl *coutrol = [[UIControl alloc] init];
        coutrol.tag = 100 + i;
        [self addSubview:coutrol];
        [coutrol mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(1);
            make.bottom.equalTo(self).offset(0);
            make.left.equalTo(self).offset(i * width);
            make.width.equalTo(@(width));
        }];
       
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.tag = 200;
        [coutrol addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] init];
        label.tag = 300;
        label.adjustsFontSizeToFitWidth = true;
        label.numberOfLines = 1;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:11];
        label.textColor = self.tintColor;
        [coutrol addSubview:label];
        
        
        imageView.image = [UIImage imageNamed:_images[i]];
        label.text = _titles[i];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(25, 25));
            make.centerX.equalTo(coutrol).offset(0);
            make.top.equalTo(coutrol).offset(4);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(coutrol).offset(0);
            make.right.equalTo(coutrol).offset(0);
            make.bottom.equalTo(coutrol).offset(0);
            make.top.equalTo(imageView.mas_bottom).offset(0);
        }];
        if (i == 0) {
          [self touchAction:coutrol];
        }
        [coutrol addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
}

#pragma mark - 点击事件
- (void)touchAction:(UIControl *)sender {
    // 第一次点击
    if (_selectedView == nil) {
        _selectedView = sender;
        [self setViewHighlight:true view:sender];
        return;
    }
    // 点击同一个
    if (_selectedView == sender) {
        return;
    }
    
    if (sender.tag == 104 || sender.tag == 103 || sender.tag == 102) {
        if (!BY_USER) {
            ByPhoneLoginVC *loginVC = [[ByPhoneLoginVC alloc] init];
            ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:loginVC];
            [self.viewController presentViewController:navi animated:YES completion:nil];
        }
    }
    
    
    [self setViewHighlight:true view:sender];
    [self setViewHighlight:false view:_selectedView];
    _selectIndex = sender.tag - 100;
    _selectedView = sender;
    if (_selectBlock) {
        _selectBlock(_selectIndex);
    }
}

- (void)setViewHighlight:(BOOL)highlight view:(UIControl *)view {
    UIImageView *imageView = [view viewWithTag:200];
    UILabel *label = [view viewWithTag:300];
    NSInteger tag = view.tag - 100;
    if (highlight) {
        imageView.image = [UIImage imageNamed:_selectedImages[tag]];
        label.textColor = self.selectedTintColor;
    } else {
        imageView.image = [UIImage imageNamed:_images[tag]];
        label.textColor = self.tintColor;
    }
}


@end
